﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using Brjnk.Common;
using Brjnk.GpxExplorer.ViewModel;

namespace Brjnk.GpxExplorer.UI
{
    /// <summary>
    /// Interaction logic for ImportWindow.xaml
    /// </summary>
    public partial class ImportExportWindow : Window, INotifyPropertyChanged
    {
        public ImportExportWindow()
        {
            InitializeComponent();
        }

        public ImportExportWindow(Data.ImportExport.DataReaderResult result)
            : this()
        {
            this.SelectedWaypoints = result.Waypoints.Select(d => new WaypointVM(d)).ToList();
            this.SelectedTracks = result.Tracks.Select(d => new TrackVM(d)).ToList();
            this.SelectedRoutes = result.Routes.Select(d => new RouteVM(d)).ToList();
            exception.DataContext = result.Exception;
            if (result.Exception != null) exception.Visibility = System.Windows.Visibility.Visible;
        }

        public ImportExportWindow(IEnumerable<WaypointVM> waypoints, IEnumerable<RouteVM> routes, IEnumerable<TrackVM> tracks)
            : this()
        {
            SelectedWaypoints = waypoints;
            SelectedTracks = tracks;
            SelectedRoutes = routes;
        }

        public IEnumerable<WaypointVM> SelectedWaypoints
        {
            get { return picker.Waypoints.Where(o => o.IsChecked).Select(o => o.Object); }
            private set
            {
                if (value == null) return;
                picker.Waypoints = value.Select(o => new CheckListItem<WaypointVM>(o, true)).ToList();
            }
        }

        public IEnumerable<RouteVM> SelectedRoutes
        {
            get { return picker.Routes.Where(o => o.IsChecked).Select(o => o.Object); }
            private set
            {
                if (value == null) return;
                picker.Routes = value.Select(o => new CheckListItem<RouteVM>(o, true)).ToList();
            }
        }

        public IEnumerable<TrackVM> SelectedTracks
        {
            get { return picker.Tracks.Where(o => o.IsChecked).Select(o => o.Object); }
            private set
            {
                if (value == null) return;
                picker.Tracks = value.Select(o => new CheckListItem<TrackVM>(o, true)).ToList();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            ObjectViewer v = new ObjectViewer();
            v.Object = exception.DataContext;
            v.Show();
        }
    }
}